/*
 *    Copyright 2009-2022 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.AAMyTest;

import org.apache.ibatis.AAMyDemo.MyBaseTest;
import org.apache.ibatis.AAMyDemo.dao.TestAnnDao;
import org.apache.ibatis.AAMyDemo.domain.Blog;
import org.apache.ibatis.AAMyDemo.domain.Mock;
import org.apache.ibatis.AAMyDemo.domain.MyTest;
import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.executor.BaseExecutor;
import org.apache.ibatis.executor.CachingExecutor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.*;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * 缓存源码
 *
 * @Author yangSen
 * @Date 2022年04月01日 16:14
 */
public class CacheSource extends MyBaseTest {


  private SqlSessionFactory factory;
  private SqlSession sqlSession;
  private Configuration configuration;


  @BeforeEach
  public void init() throws IOException {
    SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
    factory = factoryBuilder.build(Resources.getResourceAsStream(MyBaseTest.RESOURCE_PATH));
    configuration = factory.getConfiguration();
    // 二级缓存
    configuration.setCacheEnabled(true);

    sqlSession = factory.openSession();
  }

  /**
   * 查看源码
   */
  @Test
  public void test1() {
    TestAnnDao mapper = sqlSession.getMapper(TestAnnDao.class);
    /**
     * 开启二级缓存走  {@linkplain CachingExecutor#query(MappedStatement, Object, RowBounds, ResultHandler)}
     *
     * 没开启二级缓存走  {@linkplain  BaseExecutor#query(MappedStatement, Object, RowBounds, ResultHandler)}
     */
    MyTest myTest = mapper.testSelectById(10);
    MyTest myTest1 = mapper.testSelectById(10);
    System.out.println(myTest1 == myTest);
  }

  /**
   * 二级缓存是全局的,可以在不同的会话中使用
   * 二级缓存必须声明才可以使用    @CacheNamespace   <cache/>
   * 二级缓存必须提交之后才能命中缓存
   * <p>
   * 开启二级缓存走  {@linkplain CachingExecutor#query(MappedStatement, Object, RowBounds, ResultHandler)}
   * 没开启二级缓存走  {@linkplain  BaseExecutor#query(MappedStatement, Object, RowBounds, ResultHandler)}
   */
  @Test
  public void test2() {

    SqlSession sqlSession = factory.openSession();
    TestAnnDao mapper = sqlSession.getMapper(TestAnnDao.class);
    // 单个参数
//    mapper.testSelectById(10);
    // 多个参数
    mapper.testSelectByIdAndName(10, "张三");
    /**
     * {@linkplain MapperProxy#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])}
     */
//    mapper.testUpdate(10, "李四");
    /**
     * {@linkplain DefaultSqlSession#commit(boolean)}
     */
    sqlSession.commit();

    // ====================
    SqlSession sqlSession2 = factory.openSession();
    TestAnnDao mapper2 = sqlSession2.getMapper(TestAnnDao.class);
    mapper2.testSelectById(10);
  }

  /**
   * 查看缓存
   */
  @Test
  public void testCache() {
    Cache cache = configuration.getCache("org.apache.ibatis.AAMyDemo.dao.TestXmlDao");
    cache.putObject("yangsen", "yangsen");
    System.out.println(cache.getObject("yangsen"));
  }

  /**
   * 查看   MetaObject
   * <p>
   * 详细执行过程详见  图解   MetaObject执行流程.pdf
   */
  @Test
  public void testMetaObject() {
    Blog blog = Mock.newBolg();
    Configuration configuration = new Configuration();
    MetaObject metaObject = configuration.newMetaObject(blog);
    metaObject.getValue("comments[0].user.name");
  }

}
